<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="请购编号" prop="nos">
        <el-input v-model="queryParams.nos" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="请购状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择请购状态" clearable>
          <el-option v-for="dict in dict.type.please_buy_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="下单日期" prop="ddDate">
        <el-date-picker clearable v-model="queryParams.ddDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择下单日期">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="下单日期">
        <el-date-picker v-model="dateRange" style="display: flex;" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="需求日期" prop="subDate">
        <el-date-picker clearable v-model="queryParams.subDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择需求日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入客户名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddForIron"
          v-hasPermi="['develop:LvDo:add']">新增铁件请购单</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddForIronList"
          v-hasPermi="['develop:LvDo:add']">新增铁件请购单（新）</el-button>
      </el-form-item>
    </el-form>


    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
      :max-height="tableMaxHeight" @sort-change="handleSortChange">
      <el-table-column sortable label="请购编号" align="center" prop="nos" />
      <el-table-column sortable align="center" label="请购来源" prop="source">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.please_buy_source" :value="scope.row.source" />
        </template>
      </el-table-column>
      <el-table-column sortable label="下单人" align="center" prop="userName" />
      <el-table-column sortable label="客户名称" align="center" prop="customerName" show-overflow-tooltip />
      <el-table-column sortable label="下单日期" align="center" prop="ddDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ddDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="需求日期" align="center" prop="subDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.subDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="创建时间" align="center" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="请购状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.please_buy_iron_status" :value="scope.row.ironStatus" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column align="center" label="附件" prop="attachment">
        <template slot-scope="scope">
          <el-tooltip class="item" content="点击下载附件" effect="dark" placement="left-start">
            <a :href="baseUrl + scope.row.params.attachment" style="color: #15B6FF;" target="_blank" download
              v-if="scope.row.params.attachment">附件</a>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="260">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleShow(scope.row)">查看明细
          </el-button>
          <el-button @click="handlePrint(scope.row)" size="mini" type="text" icon="el-icon-printer">打印
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit"
            v-if="scope.row.ironStatus === '0' || scope.row.ironStatus === '1'" @click="handleOtherUpdate(scope.row)">
            修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit"
            v-if="scope.row.ironStatus === '0' || scope.row.ironStatus === '1'" @click="handleOtherListUpdate(scope.row)">
            修改（新）
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-check"
            v-show="scope.row.ironStatus === '0' || scope.row.ironStatus === '3'" v-hasPermi="['lvDo:lvDo:ironStoreSubmit']"
            @click="handleSubmitIronToBuy(scope.row)">提交采购</el-button>
          <el-popconfirm :title="`确认退回请购单？`" @confirm="handleIronStoreReject(scope.row)"
            v-show="scope.row.ironStatus === '0' || scope.row.ironStatus === '3'" v-hasPermi="['lvDo:lvDo:ironStoreRefuse']"
            style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-close">
              库管退回</el-button>
          </el-popconfirm>
          <el-popconfirm :title="`确认通过请购单？`" @confirm="handleIronBuyPass(scope.row)" v-show="scope.row.ironStatus === '2'"
            v-hasPermi="['lvDo:lvDo:ironBuyPass']" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-check">
              采购通过</el-button>
          </el-popconfirm>
          <el-popconfirm :title="`确认退回请购单？`" @confirm="handleIronBuyReject(scope.row)"
            v-show="scope.row.ironStatus === '2'" v-hasPermi="['lvDo:lvDo:ironBuyRefuse']" style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-close">
              采购退回</el-button>
          </el-popconfirm>
          <el-popconfirm :title="`确认删除请购单？`" @confirm="handleDelete(scope.row)" v-show="scope.row.ironStatus === '0'"
            style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete">
              删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <PleaseBuyForm-list ref="pleaseBuyFormList" @ok="getList"></PleaseBuyForm-list>
    <updateqinggou-iron-form ref="pleaseBuyUpdateIronForm" @ok="getList"></updateqinggou-iron-form>
    <addqinggou-iron-form ref="addqinggouIronForm" @ok="getList"></addqinggou-iron-form>
    <addqinggou-iron-form-list ref="addqinggouIronFormList" @ok="getList"></addqinggou-iron-form-list>
    <please-buy-detail-print ref="pleaseBuyDetailPrint"></please-buy-detail-print>
    <please-buy-iron-detail-form ref="pleaseBuyIronDetailForm" @ok="getList"></please-buy-iron-detail-form>


  </div>
</template>

<script>
import { deleteAction, getAction } from '@/api/manage'
import PleaseBuyFormList from '@/views/develop/pleaseBuy/prjForGoodsIn/PleaseBuyFormList'
import AddqinggouForm from '@/views/develop/pleaseBuy/prjForGoodsIn/AddqinggouForm'
import UpdateqinggouForm from '@/views/develop/pleaseBuy/prjForGoodsIn/UpdateqinggouForm'
import AddqinggouIronForm from "@/views/develop/pleaseBuy/pleaseBuyIron/module/AddqinggouIronForm.vue";
import PleaseBuyIronDetailForm from "@/views/develop/pleaseBuy/pleaseBuyIron/module/PleaseBuyIronDetailForm.vue";
import PleaseBuyDetailPrint from "@/views/develop/pleaseBuy/prjForGoodsIn/PleaseBuyDetailPrint.vue";
import UpdateqinggouIronForm from "@/views/develop/pleaseBuy/pleaseBuyIron/module/UpdateqinggouIronForm.vue";
import AddqinggouIronFormList from "@/views/develop/pleaseBuy/pleaseBuyIron/module/AddqinggouIronFormList.vue";

export default {
  dicts: ['please_buy_iron_status', 'please_buy_source'],
  name: "PleaseBuyIronList",
  components: {
    AddqinggouIronFormList,
    UpdateqinggouIronForm,
    PleaseBuyDetailPrint,
    PleaseBuyIronDetailForm,
    AddqinggouIronForm,
    //LvDoForm,
    PleaseBuyFormList,
    AddqinggouForm,
    UpdateqinggouForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请购单表格数据
      dataList: [],
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        mainId: null,
        nos: null,
        ddDate: null,
        subDate: null,
        type: null,
        ywType: null,
        tax: null,
        customerId: null,
        customerName: null,
        isBuy: null,
        supplierId: null,
        supplierName: null,
        deptId: null,
        deptName: null,
        userId: null,
        userName: null,
        linkman: null,
        linktel: null,
        idcard: null,
        moneycard: null,
        source: null,
        orderByColumn: 'a.nos',
        isAsc: 'desc',
      },
      url: {
        list: '/lvDo/lvDo/list',
        remove: '/lvDo/lvDo/',
        export: '/lvDo/lvDo/export',
        submitQgd: '/lvDo/lvDo/submitQgd/',
        ironBuyPass: '/lvDo/lvDo/ironBuyPass',
        ironBuyReject: '/lvDo/lvDo/ironBuyReject',
        storeReject: '/lvDo/lvDo/storeReject',
      },
      tableMaxHeight: window.innerHeight - 320,
      baseUrl: process.env.VUE_APP_BASE_API,
    };
  },
  created() {
    this.queryParams.source = this.$route.query.source;
    this.queryParams.ironStatus = this.$route.query.ironStatus;
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    handleSubmitQgd(row) {
      const ids = row.id || this.ids;
      getAction(this.url.submitQgd, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("提交成功！");
      }).catch(() => { });
    },
    handleOtherUpdate(row) {
      this.$refs.pleaseBuyUpdateIronForm.showDoList(row);
    },
    handleOtherListUpdate(row) {
      this.$refs.addqinggouIronFormList.handleUpdate(row);
    },

    handleShow(row) {
      this.$refs.pleaseBuyFormList.showDoList(row);
    },
    /** 查询请购单列表 */
    getList() {
      this.loading = true;
      this.addDateRange(this.queryParams, this.dateRange, "ApplyTime");
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm");
      this.queryParams.status = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `lvDo_${new Date().getTime()}.xlsx`)
    },

    /** 新增铁件操作 */
    handleAddForIron() {
      this.$refs.addqinggouIronForm.handleAdd();
    },

    /** 新增铁件操作 */
    handleAddForIronList() {
      this.$refs.addqinggouIronFormList.handleAdd();
    },


    /** 提交请购到采购 */
    handleSubmitIronToBuy(row) {
      this.$refs.pleaseBuyIronDetailForm.handleUpdate(row);
    },
    handleIronStoreReject(row) {
      let params = {
        id: row.id,
      }
      getAction(this.url.storeReject, params).then(response => {
        this.getList();
        this.$modal.msgSuccess("审核通过完成");
      })
    },
    handleIronBuyPass(row) {
      let params = {
        id: row.id,
      }
      getAction(this.url.ironBuyPass, params).then(response => {
        this.getList();
        this.$modal.msgSuccess("审核通过完成");
      })
    },
    handleIronBuyReject(row) {
      let params = {
        id: row.id,
      }
      getAction(this.url.ironBuyReject, params).then(response => {
        this.getList();
        this.$modal.msgSuccess("审核退回完成");
      })
    },

    handlePrint(row) {
      this.$refs.pleaseBuyDetailPrint.showDetail(row);
    },


  }
};
</script>
